package com.itcast.monkey.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.itcast.monkey.domain.base.Area;
import com.itcast.monkey.service.base.AreaService;
import com.itcast.monkey.utils.PinYin4jUtils;
import com.itcast.monkey.web.action.common.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


@ParentPackage("json-default")
@Scope("prototype")
@Namespace("/")
@Controller
public class AreaAction extends BaseAction<Area> {

	private File file;
	private String fileFileName;
	
	public void setFile(File file) {
		this.file = file;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	@Autowired
	private AreaService areaService;

	@Action(value = "area_upload")
	public String upload() throws IOException {
		List<Area> arealist = new ArrayList<>();
		if (fileFileName.endsWith(".xls")) {
			HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
			HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
			for (Row row : sheet) {
				if (row.getRowNum() == 0) {
					continue;
				} else if (row.getCell(0) == null || StringUtils.isEmpty(row.getCell(0).getStringCellValue())) {
					continue;
				}
				Area area = new Area();
				area.setId(row.getCell(0).getStringCellValue());
				area.setProvince(row.getCell(1).getStringCellValue());
				area.setCity(row.getCell(2).getStringCellValue());
				area.setDistrict(row.getCell(3).getStringCellValue());
				area.setPostcode(row.getCell(4).getStringCellValue());
				String province = row.getCell(1).getStringCellValue();
				String city = row.getCell(2).getStringCellValue();
				String district = row.getCell(3).getStringCellValue();
				province = province.substring(0, province.length() - 1);
				city = city.substring(0, city.length() - 1);
				district = district.substring(0, district.length() - 1);
				String citycode = PinYin4jUtils.hanziToPinyin(city);
				String[] strs = PinYin4jUtils.getHeadByString(province + city + district);
				StringBuffer str = new StringBuffer();
				for (String st : strs) {
					str.append(st);
				}
				area.setShortcode(str.toString());
				area.setCitycode(citycode);

				arealist.add(area);
			}
		} else if (fileFileName.endsWith(".xlsx")) {
			XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(file));
			XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
			for (Row row : sheet) {
				if (row.getRowNum() == 0) {
					continue;
				} else if (row.getCell(0) == null || StringUtils.isEmpty(row.getCell(0).getStringCellValue())) {
					continue;
				}
				Area area = new Area();
				area.setId(row.getCell(0).getStringCellValue());
				area.setProvince(row.getCell(1).getStringCellValue());
				area.setCity(row.getCell(2).getStringCellValue());
				area.setDistrict(row.getCell(3).getStringCellValue());
				area.setPostcode(row.getCell(4).getStringCellValue());
				String province = row.getCell(1).getStringCellValue();
				String city = row.getCell(2).getStringCellValue();
				String district = row.getCell(3).getStringCellValue();
				province = province.substring(0, province.length() - 1);
				city = city.substring(0, city.length() - 1);
				district = district.substring(0, district.length() - 1);
				String citycode = PinYin4jUtils.hanziToPinyin(city);
				String[] strs = PinYin4jUtils.getHeadByString(province + city + district);
				StringBuffer str = new StringBuffer();
				for (String st : strs) {
					str.append(st);
				}
				area.setShortcode(str.toString());
				area.setCitycode(citycode);
				arealist.add(area);
			}
		}
		areaService.save(arealist);
		return NONE;
	}

	// 分页查询
	@Action(value = "area_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		Pageable pageable = new PageRequest(page-1, rows);
		Specification<Area> spercification = new Specification<Area>() {
			List<Predicate> list = new ArrayList<>();
			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				if (!StringUtils.isEmpty(model.getProvince())) {
					Predicate p1 = cb.like(root.get("province").as(String.class), "%" + model.getProvince() + "%");
					list.add(p1);
				}
				;
				if (!StringUtils.isEmpty(model.getCity())) {
					Predicate p2 = cb.like(root.get("city").as(String.class), "%" + model.getCity() + "%");
					list.add(p2);
				}
				;
				if (!StringUtils.isEmpty(model.getDistrict())) {
					Predicate p3 = cb.like(root.get("district").as(String.class), "%" + model.getDistrict() + "%");
					list.add(p3);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page<Area> pagelist = areaService.findAll(spercification, pageable);
		this.pushValueStack(pagelist);
		return SUCCESS;
	}

}
